<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('shop.order.evaluate.quick Search Fields') })"
        >
            <template #scoreSearch>
                <StatusWithBadge
                    field="score"
                    :data="[
                        {
                            name: '差评',
                            value: 'bad',
                            count: 0,
                        },
                        {
                            name: '中评',
                            value: 'normal',
                            count: 0,
                        },
                        {
                            name: '好评',
                            value: 'good',
                            count: 0,
                        },
                    ]"
                    v-model="baTable.comSearch.form.score"
                />
            </template>
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef">
            <template #goods_name>
                <el-table-column :label="t('shop.order.evaluate.goods_name')" align="center" min-width="240" :show-overflow-tooltip="true">
                    <template #default="{ row }">
                        <el-tooltip :visible="row.visible" placement="top">
                            <template #content>
                                <span>{{ row.goods_spec }}</span>
                            </template>
                            <Icon name="fa fa-info-circle" @mouseenter="row.visible = true" @mouseleave="row.visible = false" />
                        </el-tooltip>
                        {{ row.goods_name }}
                    </template>
                </el-table-column>
            </template>

            <template #buyer>
                <el-table-column :label="t('shop.order.evaluate.user_name')" align="center" :show-overflow-tooltip="true" min-width="150">
                    <template #default="{ row }">
                        <view class="buyer">
                            <div>
                                <el-avatar class="image" :size="40" :src="fullUrl(row.user_avatar)" />
                            </div>
                            <div style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden; word-break: break-all">
                                {{ row.user_name }}
                            </div>
                        </view>
                    </template>
                </el-table-column>
            </template>

            <template #score>
                <el-table-column :label="t('shop.order.evaluate.score')" align="center" min-width="140">
                    <template #default="{ row }">
                        <el-rate v-model="row.score" disabled />
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <detailDialog v-if="baTable.table.extend!.showDetailDialog" />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import detailDialog from './detailDialog.vue'
import { fullUrl } from '/@/utils/common'
import StatusWithBadge from '/@/components/shop/table/statusWithBadge.vue'

defineOptions({
    name: 'shop/order/evaluate',
})

const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons([])

optButtons.push({
    render: 'tipButton',
    name: 'detail',
    title: 'shop.order.evaluate.detail',
    type: 'warning',
    icon: 'fa fa-search-plus icon',
    click: (row) => {
        baTable.table.extend!.showDetailDialog = true
        baTable.table.extend!.detailData = row
    },
})

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/shop.order.Evaluate/'),
    {
        pk: 'id',
        column: [
            { label: t('shop.order.evaluate.id'), prop: 'id', align: 'center', width: 70, operator: false, sortable: 'custom' },
            {
                label: t('shop.order.evaluate.type'),
                prop: 'score',
                align: 'center',
                render: 'tag',
                replaceValue: {
                    '1': '差评',
                    '2': '差评',
                    '3': '中评',
                    '4': '好评',
                    '5': '好评',
                },
                custom: {
                    '1': 'danger',
                    '2': 'danger',
                    '3': 'warning',
                    '4': 'success',
                    '5': 'success',
                },
                comSearchRender: 'slot',
                comSearchSlotName: 'scoreSearch',
                comSearchColAttr: { xs: 24, sm: 24 },
                width: 100,
            },
            {
                label: t('shop.order.evaluate.goods_name'),
                prop: 'goods_name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                render: 'slot',
                slotName: 'goods_name',
            },
            {
                render: 'slot',
                slotName: 'score',
                operator: false,
            },
            {
                label: t('shop.order.evaluate.user_name'),
                prop: 'user_name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                render: 'slot',
                slotName: 'buyer',
            },
            {
                label: t('shop.order.evaluate.status'),
                prop: 'status',
                align: 'center',
                operator: 'eq',
                sortable: false,
                replaceValue: { 0: t('shop.order.evaluate.status 0'), 1: t('shop.order.evaluate.status 1') },
                render: 'customSwitch',
                comSearchRender: 'select',
                custom: {
                    activeText: t('shop.order.evaluate.status 1'),
                    inactiveText: t('shop.order.evaluate.status 0'),
                },
                width: 100,
            },
            {
                label: t('shop.order.evaluate.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            { label: t('Operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false, fixed: 'right' },
        ],
        dblClickNotEditColumn: ['all'],
    },
    {
        defaultItems: {},
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss">
.buyer {
    display: flex;
    flex-direction: row;
    align-items: center;
    // justify-content: center;

    .image {
        margin-right: 10px;
    }
}

@media screen and (min-width: 768px) {
    :deep(.table-com-search .score .com-search-col-label) {
        width: 8.3%;
    }
}
</style>
